package com.datatest.riskcheck.tools;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;

/**
 * @Author lxt
 * Created by Jccfc on 2021/10/15.
 */
public class TestJavaxXml {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        String path="D:\\xml文件.xml";
        File file = new File(path);
        getContent(file);
    }
    private static void getContent(File path) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
        //创建解析工厂
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        //创建解析器
        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
        //生成对象叔
        Document document = builder.parse(path);
        //创建xpath对象
        XPath xPath = XPathFactory.newInstance().newXPath();
//        String titleXpath="//@content";
        NodeList bookList = (NodeList) xPath.evaluate("//@content", document, XPathConstants.NODESET);
        //System.out.println(bookList.getLength());
        for(int i = 0; i < bookList.getLength(); i++){
            if(bookList.item(i).getNodeType() != Node.ELEMENT_NODE) {
                String bookElt =  bookList.item(i).toString();
                //System.out.println(bookElt);
                System.out.println(bookList.item(i).getTextContent());
            }
        }


    }

}

